package com.manager.plat.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.manager.plat.dao.ILimitUserDao;
import com.manager.plat.util.BaseUtil;
import com.manager.plat.vo.LimitUserVo;

public class LimitUserDaoImpl extends JdbcDaoSupport implements ILimitUserDao {
	private static Logger logger = Logger.getLogger(LimitUserDaoImpl.class);
	
	/**获得用户总量限制列表*/
	@SuppressWarnings("unchecked")
	@Override
	public List<LimitUserVo> getLimitUserList() {
		String sql = "select id, day_price, month_price, status from tb_ivr_limit_user where status = 1";
		logger.info(String.format("获得用户总量限制列表[%s]", sql));
		
		List<LimitUserVo> limitUserVoList = this.getJdbcTemplate().query(sql, rowMapper);
		if(limitUserVoList != null && limitUserVoList.size() > 0){
			return limitUserVoList;
		}
		return null;
	}
	/**根据Id获得用户总量限制信息*/
	@SuppressWarnings("unchecked")
	@Override
	public LimitUserVo getLimitUserById(int id) {
		String sql = "select id, day_price, month_price, status from tb_ivr_limit_user where id = ? and status = 1";
		Object[] param = new Object[]{id};
		logger.info(String.format("根据Id获得用户总量限制信息[%s]", BaseUtil.logSQL(sql, param)));
		
		List<LimitUserVo> limitUserVoList =this.getJdbcTemplate().query(sql, param, rowMapper);
		if(limitUserVoList != null && limitUserVoList.size() > 0){
			return limitUserVoList.get(0);
		}
		return null;
	}
	/**数据转换*/
	private ParameterizedRowMapper<LimitUserVo> rowMapper = new ParameterizedRowMapper<LimitUserVo>() {
		@Override
		public LimitUserVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			LimitUserVo limitUserVo = new LimitUserVo();
			limitUserVo.setId(rs.getInt("id"));
			limitUserVo.setDay_price(rs.getInt("day_price"));
			limitUserVo.setMonth_price(rs.getInt("month_price"));
			limitUserVo.setStatus(rs.getInt("status"));
			return limitUserVo;
		}
	};
	
	/**新增用户总量限制信息*/
	@Override
	public int insertLimitUser(LimitUserVo limitUserVo) {
		String sql = "insert into tb_ivr_limit_user(day_price, month_price, status, add_user) values(?,?,1,?)";
		Object[] param = new Object[]{limitUserVo.getDay_price(), limitUserVo.getMonth_price(), limitUserVo.getAdd_user()};
		logger.info(String.format("新增用户总量限制信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**更新用户总量限制信息*/
	@Override
	public int updateLimitUser(LimitUserVo limitUserVo) {
		String sql = "update tb_ivr_limit_user set day_price = ?, month_price = ?, add_user = ? where id = ?";
		Object[] param = new Object[]{limitUserVo.getDay_price(), limitUserVo.getMonth_price(), 
									limitUserVo.getAdd_user(),limitUserVo.getId()};
		logger.info(String.format("更新用户总量限制信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**删除用户总量限制信息*/
	@Override
	public int deleteLimitUser(int id) {
		String sql = "update tb_ivr_limit_user set status = 0 where id = ?";
		Object[] param = new Object[]{id};
		logger.info(String.format("删除用户总量限制信息[%s]", BaseUtil.logSQL(sql, param)));

		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
}
